﻿using System;
using System.Collections.Generic;
using System.Collections;

using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;

public partial class admin_CompanyInfo_Account : System.Web.UI.Page
{
    public ArrayList accountarr = null;
    public int id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            accountarr = (ArrayList)DB.getArrCuenta("");
            llenocompanias();

            if (Request.QueryString["id"] != null) id = int.Parse(Request.QueryString["id"].ToString());

            lb_cuentaid.Text = id.ToString();

            RE_GenericBean cuenta = DB.getCuenta(id);

            lb_cuentaid.Text = cuenta.intC1.ToString();
            ddl_company.SelectedValue = cuenta.intC2.ToString();
            tb_accountname.Text = cuenta.strC1;
            tb_accountdesc.Text = cuenta.strC2;
            tb_operationmanager.Text = cuenta.strC3;

            if (cuenta.intC1 != 0)
            {
                habilitocampos(false);
                HyperLink1_ModalPopupExtender.Show();
            }
            tb_numberemployee.Text = DB.getCountEmployeebyCuenta(id).ToString();
        }
        else { 
            id=int.Parse(lb_cuentaid.Text);
        }


    }

    private void habilitocampos(bool estado)
    {
        tb_accountdesc.Enabled = estado;
        tb_accountname.Enabled = estado;
        //tb_numberemployee.Enabled = estado;
        tb_operationmanager.Enabled = estado;
        ddl_company.Enabled = estado;
    }

    private void llenocompanias()
    {
        ArrayList companyarr = null;
        ListItem item = null;
        try
        {
            companyarr = (ArrayList)DB.getArrCompania("");
            if (companyarr != null && companyarr.Count > 0)
            {
                foreach (RE_GenericBean company in companyarr)
                {
                    item = new ListItem(company.strC1, company.intC1.ToString());
                    ddl_company.Items.Add(item);
                }
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        RE_GenericBean accounts = new RE_GenericBean();
        bool edit = false;
        accounts.intC1 = int.Parse(lb_cuentaid.Text);
        accounts.intC2 = int.Parse(ddl_company.SelectedValue.ToString());
        accounts.strC1 = tb_accountname.Text.Trim().ToUpper();
        accounts.strC2 = tb_accountdesc.Text.Trim().ToUpper();
        accounts.strC3 = tb_operationmanager.Text.Trim().ToUpper();

        if (accounts.intC1 != 0) edit = true;

        if (DB.InsertAccount(accounts, edit))
        {
            habilitocampos(false);
            Response.Redirect("CompanyInfo_Account.aspx");
        }
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        habilitocampos(true);
        HyperLink1_ModalPopupExtender.Show();
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        int id = int.Parse(lb_cuentaid.Text);
        if (id != 0)
        {
            int result = DB.DeleteCuenta(id);
            Response.Redirect("CompanyInfo_Account.aspx");
        }
    }
}